package org.link.ui;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JRadioButton;
import javax.swing.JButton;

import org.link.util.UserAllData;
import org.link.util.Util;
/**
 * 
 * project_name:jksystem
 * package_name:org.link.ui
 * file_name:ChapterFrame.java
 * @author Link
 * create_date:2015年9月19日下午8:30:35
 * modified_date:
 * description: 章节练习窗体类
 *
 */
public class ChapterFrame extends JFrame {

	private JPanel mainPane;				//主面板
	private JLabel tittleLabel;				//题目标签
	private JLabel aOptionLabel;			//A选项标签
	private JRadioButton aRadioButton;		//A选项按钮
	private JRadioButton bRadioButton;		//B选项按钮
	private JRadioButton cRadioButton;		//C选项按钮
	private JRadioButton dRadioButton;		//D选项按钮
	private JButton nextButton;				//下一题按钮
	private JButton frontButton;			//上一题按钮
	private JLabel imageLabel;				//图片标签
	private JButton openButton;				//展开按钮
	private JButton closeButton;			//收缩按钮
	private JLabel dOptionLabel;			//D选项标签
	private JLabel cOptionLabel;			//C选项标签
	private JLabel bOptionLabel;			//B选项标签
	private ButtonGroup buttonGroup;		//按钮群
	private int start,end;					//起始参数
	private JPanel upPanel;					//标题图片选项面板
	private JPanel downPanel;				//试题展开面板
	private JLabel rightpromptLabel;		//提示标签
	private JLabel wrongpromptLabel;		//错误提示标签
	private JPanel questionPanel;			//题目面板
	private JButton tittleButton;			//题目按钮
	private TittleActionListener tittleActionListener = new TittleActionListener();
	private int num;
	
	public ChapterFrame(){
		super();
	}

	/**
	 * Create the frame.
	 */
	public ChapterFrame(int start,int end) {
		this.start = start;
		this.end = end;
		this.num = start;
		setSize(763, 479);
		Util.screen(this);
		setResizable(false);
		setTitle("小车_科目一_章节练习");
		mainPane = new JPanel();
		mainPane.setBackground(Color.WHITE);
		mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		mainPane.setBounds(0, 0, 763, 479);
		this.add(mainPane);
		mainPane.setLayout(null);
		this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent arg0) {
				ChapterFrame.this.setVisible(false);
				
			}
			
			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		tittleLabel = new JLabel();
		tittleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		tittleLabel.setBounds(10, 10, 657, 49);
		tittleLabel.setText(UserAllData.chapters.get(ChapterFrame.this.start).getQuesId()+" "+UserAllData.chapters.get(ChapterFrame.this.start).getQuesTittle());
		
		/**
		 * A选项
		 */
		aRadioButton = new JRadioButton("A");
		aRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		aRadioButton.setBounds(47, 83, 44, 30);
		aRadioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(aRadioButton.isSelected()){
					if(UserAllData.chapters.get(ChapterFrame.this.start).getRightAnswer().equals("A")){
						updateDownPanel();
						if(ChapterFrame.this.start == ChapterFrame.this.end){
							JOptionPane.showMessageDialog(null, "已经是最后一题");
							return;
						}else{
							try {
								Thread.sleep(600);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							updateUpPanel(ChapterFrame.this.start+1);//调用更新标题,答案选项,图片公共方法
							ChapterFrame.this.start++;
						}
					}else{
						updateDownPanel(ChapterFrame.this.start);
					}
				}
			}
		});
		aRadioButton.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				aRadioButton.setSelected(true);
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		/**
		 * B选项
		 */
		bRadioButton = new JRadioButton("B");
		bRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		bRadioButton.setBounds(47, 121, 44, 30);
		bRadioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(bRadioButton.isSelected()){
					if(UserAllData.chapters.get(ChapterFrame.this.start).getRightAnswer().equals("B")){
						updateDownPanel();
						if(ChapterFrame.this.start == ChapterFrame.this.end){
							JOptionPane.showMessageDialog(null, "已经是最后一题");
							return;
						}else{
							try {
								Thread.sleep(600);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							updateUpPanel(ChapterFrame.this.start+1);//调用更新标题,答案选项,图片公共方法
							ChapterFrame.this.start++;
						}
					}else{
						updateDownPanel(ChapterFrame.this.start);
					}
				}
			}
		});
		bRadioButton.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				bRadioButton.setSelected(true);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		/**
		 * C选项
		 */
		cRadioButton = new JRadioButton("C");
		cRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		cRadioButton.setBounds(47, 163, 44, 30);
		cRadioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(cRadioButton.isSelected()){
					if(UserAllData.chapters.get(ChapterFrame.this.start).getRightAnswer().equals("C")){
						updateDownPanel();
						if(ChapterFrame.this.start == ChapterFrame.this.end){
							JOptionPane.showMessageDialog(null, "已经是最后一题");
							return;
						}else{
							try {
								Thread.sleep(600);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							updateUpPanel(ChapterFrame.this.start+1);//调用更新标题,答案选项,图片公共方法
							ChapterFrame.this.start++;
						}
					}else{
						updateDownPanel(ChapterFrame.this.start);
					}
				}
			}
		});
		cRadioButton.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				cRadioButton.setSelected(true);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		/**
		 * D选项
		 */
		dRadioButton = new JRadioButton("D");
		dRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		dRadioButton.setBounds(47, 209, 44, 30);
		dRadioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(dRadioButton.isSelected()){
					if(UserAllData.chapters.get(ChapterFrame.this.start).getRightAnswer().equals("D")){
						updateDownPanel();//调用更新下面版方法
						if(ChapterFrame.this.start == ChapterFrame.this.end){
							JOptionPane.showMessageDialog(null, "已经是最后一题");
							return;
						}else{
							try {
								Thread.sleep(600);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							updateUpPanel(ChapterFrame.this.start+1);//调用更新标题,答案选项,图片公共方法
							ChapterFrame.this.start++;
						}
					}else{
						updateDownPanel(ChapterFrame.this.start);
					}
				}
			}
		});
		dRadioButton.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				dRadioButton.setSelected(true);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		buttonGroup = new ButtonGroup();
		buttonGroup.add(aRadioButton);
		buttonGroup.add(bRadioButton);
		buttonGroup.add(cRadioButton);
		buttonGroup.add(dRadioButton);
		
		aOptionLabel = new JLabel();
		aOptionLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		aOptionLabel.setBounds(111, 83, 400, 30);
		aOptionLabel.setText(UserAllData.chapters.get(ChapterFrame.this.start).getQuesOptionA());
		
		bOptionLabel = new JLabel("New label");
		bOptionLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		bOptionLabel.setBounds(111, 121, 400, 30);
		bOptionLabel.setText(UserAllData.chapters.get(ChapterFrame.this.start).getQuesOptionB());
		
		cOptionLabel = new JLabel();
		cOptionLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		cOptionLabel.setBounds(111, 163, 400, 30);
		cOptionLabel.setText(UserAllData.chapters.get(ChapterFrame.this.start).getQuesOptionC());
		
		dOptionLabel = new JLabel();
		dOptionLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		dOptionLabel.setBounds(111, 209, 400, 30);
		dOptionLabel.setText(UserAllData.chapters.get(ChapterFrame.this.start).getQuesOptionD());
		
		/**
		 * 下一题按钮
		 */
		nextButton = new JButton("下一题");
		nextButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		nextButton.setBounds(206, 145, 113, 38);
		nextButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(ChapterFrame.this.start == ChapterFrame.this.end){
					JOptionPane.showMessageDialog(null, "已经是最后一题");
					return;
				}else{
					updateUpPanel(ChapterFrame.this.start+1);
					rightpromptLabel.setText("");
					wrongpromptLabel.setText("");//调用更新标题,答案选项,图片公共方法
					ChapterFrame.this.start++;
				}
				
			}
		});
		
		/**
		 * 上一题按钮
		 */
		frontButton = new JButton("上一题");
		frontButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		frontButton.setBounds(47, 145, 113, 38);
		frontButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(ChapterFrame.this.start == num){
					JOptionPane.showMessageDialog(null, "已经是第一题");
					return;
				}else{
					rightpromptLabel.setText("");
					wrongpromptLabel.setText("");
					updateUpPanel(ChapterFrame.this.start-1);//调用更新标题,答案选项,图片公共方法
					ChapterFrame.this.start--;
				}
				
			}
		});
		/**
		 * 试题展开按钮
		 */
		openButton = new JButton("展开试题卡");
		openButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		openButton.setBounds(554, 145, 145, 38);
		openButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				downPanel.removeAll();
				downPanel.repaint();
				frontButton.setBounds(47, 60, 113, 38);
				nextButton.setBounds(206, 60, 113, 38);
				downPanel.add(questionPanel);
				downPanel.add(frontButton);
				downPanel.add(closeButton);
				downPanel.add(nextButton);
				
			}
		});
		
		/**
		 * 试题收缩卡
		 */
		closeButton = new JButton("收缩试题卡");
		closeButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		closeButton.setBounds(554, 60, 145, 38);
		closeButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				downPanel.removeAll();
				downPanel.repaint();
				frontButton.setBounds(47, 145, 113, 38);
				nextButton.setBounds(206, 145, 113, 38);
				downPanel.add(frontButton);
				downPanel.add(openButton);
				downPanel.add(nextButton);
			}
		});
		
		imageLabel = new JLabel();
		imageLabel.setBounds(520, 69, 235, 160);
		
		upPanel = new JPanel();
		upPanel.setLayout(null);
		upPanel.setBounds(0, 0, 763, 250);
		upPanel.add(aOptionLabel);
		upPanel.add(aRadioButton);
		upPanel.add(bOptionLabel);
		upPanel.add(bRadioButton);
		upPanel.add(tittleLabel);
		if(UserAllData.chapters.get(ChapterFrame.this.start).getQuesOptionC()!=null){//C选项不为空
			upPanel.add(cOptionLabel);
			upPanel.add(cRadioButton);
			upPanel.add(dOptionLabel);
			upPanel.add(dRadioButton);
		}
		if(UserAllData.chapters.get(ChapterFrame.this.start).getImageAddress()!=null){//图片地址不为空
			ImageIcon tp = new ImageIcon(ChapterFrame.class.getResource(UserAllData.chapters.get(ChapterFrame.this.start).getImageAddress()));
			imageLabel.setIcon(tp);
			upPanel.add(imageLabel);
		}
		mainPane.add(upPanel);
		rightpromptLabel = new JLabel();
		rightpromptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		rightpromptLabel.setBounds(50, 100, 80, 30);
		rightpromptLabel.setForeground(Color.blue);
		
		wrongpromptLabel = new JLabel();
		wrongpromptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		wrongpromptLabel.setBounds(140, 100, 100, 30);
		wrongpromptLabel.setForeground(Color.red);
		
		/**
		 * 题目面板
		 */
		questionPanel = new JPanel();
		questionPanel.setBounds(35, 135, 700, 30);
		questionPanel.setLayout(new GridLayout(1, 10, 3, 3));
		for(int i = 1;i<11;i++){
			tittleButton = new JButton(i+"");
			tittleButton.addActionListener(tittleActionListener);//调用内部类按钮事件监听
			questionPanel.add(tittleButton);
		}
		downPanel = new JPanel();
		downPanel.setBounds(0, 250, 763, 229);
		downPanel.setLayout(null);
		downPanel.add(nextButton);
		downPanel.add(frontButton);
		downPanel.add(openButton);
		mainPane.add(downPanel);
	}
	
	/**
	 * 
	 * project_name:jksystem
	 * package_name:org.link.ui
	 * file_name:ChapterFrame.java
	 * @author Link
	 * create_date:2015年9月19日下午11:07:47
	 * modified_date:
	 * description: 题目内部类
	 *
	 */
	class TittleActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			JButton btn = (JButton) e.getSource();
			int num = Integer.parseInt(btn.getText())-1;
			int num1 = ChapterFrame.this.start/10;
			int num2 = num1*10+num;
			rightpromptLabel.setText("");
			wrongpromptLabel.setText("");
			updateUpPanel(num2);//调用更新标题,答案选项,图片公共方法
			ChapterFrame.this.start=num2;
		}	
	}
	
	/**
	 * 
	 * description: 更新标题,选项,图片面板公共方法
	 * creatTime:2015年9月19日下午11:35:10
	 * @author Link
	 * return type: void
	 *
	 */
	public void updateUpPanel(int num){
		upPanel.removeAll();
		upPanel.repaint();
		buttonGroup.clearSelection();
		tittleLabel.setText(UserAllData.chapters.get(num).getQuesId()+" "+UserAllData.chapters.get(num).getQuesTittle());
		aOptionLabel.setText(UserAllData.chapters.get(num).getQuesOptionA());
		bOptionLabel.setText(UserAllData.chapters.get(num).getQuesOptionB());
		cOptionLabel.setText(UserAllData.chapters.get(num).getQuesOptionC());
		dOptionLabel.setText(UserAllData.chapters.get(num).getQuesOptionD());
		upPanel.add(aOptionLabel);
		upPanel.add(aRadioButton);
		upPanel.add(bOptionLabel);
		upPanel.add(bRadioButton);
		upPanel.add(tittleLabel);
		if(UserAllData.chapters.get(num).getQuesOptionC()!=null){//C选项不为空
			upPanel.add(cOptionLabel);
			upPanel.add(cRadioButton);
			upPanel.add(dOptionLabel);
			upPanel.add(dRadioButton);
		}
		if(UserAllData.chapters.get(num).getImageAddress()!=null){//图片地址不为空
			ImageIcon tp = new ImageIcon(ChapterFrame.class.getResource(UserAllData.chapters.get(num).getImageAddress()));
			imageLabel.setIcon(tp);
			upPanel.add(imageLabel);
		}	
	}
	/**
	 * 
	 * description: 判断正确时更新下面版公共方法
	 * creatTime:2015年9月19日下午11:47:01
	 * @author Link
	 * return type: void
	 *
	 */
	public void updateDownPanel(){
		if(closeButton.isShowing()){
			downPanel.removeAll();
			downPanel.repaint();
			frontButton.setBounds(47, 60, 113, 38);
			nextButton.setBounds(206, 60, 113, 38);
			downPanel.add(frontButton);
			downPanel.add(nextButton);
			rightpromptLabel.setText("回答正确!");
			rightpromptLabel.setBounds(50, 15, 80, 30);
			downPanel.add(rightpromptLabel);
			downPanel.add(closeButton);
			downPanel.add(questionPanel);
		}else{
			downPanel.removeAll();
			downPanel.repaint();
			frontButton.setBounds(47, 145, 113, 38);
			nextButton.setBounds(206, 145, 113, 38);
			downPanel.add(frontButton);
			downPanel.add(nextButton);
			rightpromptLabel.setText("回答正确!");
			rightpromptLabel.setBounds(50, 100, 80, 30);
			downPanel.add(openButton);
			downPanel.add(rightpromptLabel);
		}
	}
	
	/**
	 * 
	 * description: 判断错误时更新下面面板公共方法
	 * creatTime:2015年9月20日上午12:24:45
	 * @author Link
	 * return type: void
	 *
	 */
	public void updateDownPanel(int num){
		if(closeButton.isShowing()){
			downPanel.removeAll();
			downPanel.repaint();
			frontButton.setBounds(47, 60, 113, 38);
			nextButton.setBounds(206, 60, 113, 38);
			downPanel.add(frontButton);
			downPanel.add(nextButton);
			rightpromptLabel.setText("回答错误!");
			rightpromptLabel.setBounds(50, 15, 80, 30);
			wrongpromptLabel.setText("正确答案: "+UserAllData.chapters.get(num).getRightAnswer());
			wrongpromptLabel.setBounds(140, 15, 100, 30);
			downPanel.add(rightpromptLabel);
			downPanel.add(wrongpromptLabel);
			downPanel.add(closeButton);
			downPanel.add(questionPanel);
		}else{
			downPanel.removeAll();
			downPanel.repaint();
			frontButton.setBounds(47, 145, 113, 38);
			nextButton.setBounds(206, 145, 113, 38);
			downPanel.add(frontButton);
			downPanel.add(nextButton);
			rightpromptLabel.setText("回答错误!");
			rightpromptLabel.setBounds(50, 100, 80, 30);
			wrongpromptLabel.setText("正确答案: "+UserAllData.chapters.get(num).getRightAnswer());
			wrongpromptLabel.setBounds(140, 100, 100, 30);
			downPanel.add(rightpromptLabel);
			downPanel.add(wrongpromptLabel);
			downPanel.add(openButton);
		}
	}
}





